package cn.fotoit.product.system.controller;
import cn.fotoit.product.system.entity.Role;
import cn.fotoit.product.system.service.RoleService;
import cn.fotoit.product.util.LogInfo;
import cn.fotoit.product.util.ReturnBean;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "角色接口", tags = "系统角色相关接口，角色添加，修改，删除，查询")
@RestController
@CrossOrigin
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("add")
    @LogInfo( info = "角色添加",saveRequestData = true,saveResponseData = true)
    @RequiresPermissions("system_role_add")
    public ReturnBean add(
            @RequestParam String roleKey,
            @RequestParam String name,
            @RequestParam String remark) {
        Role role = new Role();
        role.setRoleKey(roleKey);
        role.setName(name);
        role.setRemark(remark);
        roleService.add(role);
        return ReturnBean.success("添加成功");
    }

    @PostMapping("updateNotNull")
    @LogInfo( info = "角色修改",saveRequestData = true,saveResponseData = true)
    @RequiresPermissions("system_role_update")
    public ReturnBean updateNotNull(
            @RequestParam Integer id,
            String roleKey,
            String name,
            String remark) {
        Role role = new Role();
        role.setId(id);
        role.setRoleKey(roleKey);
        role.setName(name);
        role.setRemark(remark);
        roleService.updateNotNull(role);
        return ReturnBean.success("修改成功");
    }

    @PostMapping("deleteById")
    @LogInfo( info = "根据id删除角色",saveRequestData = true,saveResponseData = true)
    @RequiresPermissions("system_role_delete")
    public ReturnBean deleteById(@RequestParam Integer id) {
        roleService.deleteById(id);
        return ReturnBean.success("删除成功");
    }

    @PostMapping("findAll")
    @RequiresPermissions("system_role_list")
    public ReturnBean findAll(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size) {
        return ReturnBean.success("查询成功", roleService.findAll(page, size));
    }

    @PostMapping("findById")
    @RequiresPermissions("system_role_list")
    public ReturnBean findById(
            @RequestParam Integer id) {
        return ReturnBean.success("查询成功", roleService.findById(id));
    }


}
